var BackofficeREST = mongoose.model('backoffice_rests');
var async = require('async');


exports.createRESTService = function (input, callback) {
    var rest = new BackofficeREST();
    rest.displayOrder = input.displayOrder;
    rest.domain = input.domain;
    rest.httpMethod = input.httpMethod;
    rest.restURL = input.restURL;
    rest.functionDesc = input.functionDesc;
    rest.comments = input.comments;
    rest.reqMessageSample = input.reqMessageSample;
    rest.resMessageSample = input.resMessageSample;
    rest.save(callback);
};

exports.updateRESTService = function (id, input, callback) {
    BackofficeREST.update({_id: id},
        {$set:{
            'domain':input.domain,
            'restURL':input.restURL,
            'httpMethod':input.httpMethod,
            'functionDesc':input.functionDesc,
            'comments':input.comments,
            'resMessageSample':input.resMessageSample,
            'reqMessageSample':input.reqMessageSample
        }
    }, callback);
}

exports.getRESTServiceList = function(startPage,pageSize,callback){
    async.series([
            function(callbackInner){
                BackofficeREST.count({},callbackInner);
            },
            function(callbackInner){
                BackofficeREST.find({}).sort('domain').skip(startPage).limit(pageSize).exec(callbackInner);
            }
    ],callback );
}

exports.deleteRESTService = function (id, callback) {
    BackofficeREST.findOneAndRemove({_id:id}, callback);
};

exports.getRESTServiceById = function(id,callback){
    BackofficeREST.findOne({_id: id},function(err,results){
        if(results!=null){
            callback(null, results);
        }
    });
}
